﻿using System;
using System.Collections.Generic;

namespace Flagwind.Data
{

	#region 委托定义

	public delegate Comparer CompareFunc(Comparer cmp);

	public delegate Comparer CompareFunc<T>(Comparer cmp, T p);

	public delegate Comparer CompareFunc<T1, T2>(Comparer cmp, T1 p1, T2 p2);

	public delegate Comparer CompareFunc<T1, T2, T3>(Comparer cmp, T1 p1, T2 p2, T3 p3);

	#endregion

	/// <summary>
	/// 实体对象条件比较类，用于复杂条件比较表达式。
	/// </summary>
	public class Comparer
	{
		#region 保护属性

		protected string FieldName
		{
			get;
			set;
		}

		protected string ParameterName
		{
			get;
			set;
		}

		protected string Format
		{
			get;
			set;
		}

		protected CompareOperator Operator
		{
			get;
			set;
		}

		protected Comparer LeftNode
		{
			get;
			set;
		}

		protected Comparer RightNode
		{
			get;
			set;
		}

		protected CompareLogic Logic
		{
			get;
			set;
		}

		protected bool IsLeaf
		{
			get
			{
				return object.Equals(LeftNode, null) && object.Equals(RightNode, null);
			}
		}

		#endregion

		#region 公共属性

		public Command Command
		{
			get;
			private set;
		}

		public string WhereString
		{
			get
			{
				string result = string.Empty;
				if(this.IsLeaf)
				{
					//假设左边是字段名，右边是值或者其它字段名
					result = string.Format("{0} {1} {2}", GetFieldString(this.Format, this.FieldName), this.GetOperatorString(), this.ParameterName);
				}
				else if(this.Logic == CompareLogic.Not)
				{
					result = string.Format(" NOT ({0}) ", this.LeftNode.WhereString);
				}
				else
				{
					string format;
					if(this.LeftNode.IsLeaf && this.RightNode.IsLeaf)
					{
						format = " {0} {1} {2} ";
					}
					else if(this.LeftNode.IsLeaf && !this.RightNode.IsLeaf)
					{
						if(this.RightNode.Logic == this.Logic)
						{
							format = " {0} {1} {2}\r\n ";
						}
						else
						{
							format = " {0} \r\n\t{1}\r\n\t  (\r\n\t  {2}\r\n\t  )\r\n ";
						}
					}
					else if(!this.LeftNode.IsLeaf && this.RightNode.IsLeaf)
					{
						if(this.LeftNode.Logic == this.Logic)
						{
							format = " {0} {1} {2} ";
						}
						else
						{
							format = "\r\n\t  (\r\n\t  {0}\r\n\t  ) \r\n\t{1} \r\n\t{2} ";
						}
					}
					else
					{
						//左右子节点，都不是叶子结点
						bool leftFlag = CheckChildLogic(this.LeftNode, this.Logic);
						bool rightFlag = CheckChildLogic(this.RightNode, this.Logic);
						if(leftFlag && rightFlag)
						{
							format = " {0} {1} {2} ";
						}
						else if(!leftFlag && rightFlag)
						{
							format = "\r\n\t({0}) {1} {2} ";
						}
						else if(leftFlag && !rightFlag)
						{
							format = " {0} {1} ({2})\r\n ";
						}
						else
						{
							format = "\r\n\t({0})\r\n {1} \r\n\t({2})\r\n ";
						}
					}
					string logicString = this.Logic == CompareLogic.And ? "AND" : (this.Logic == CompareLogic.Or ? "OR" : "NOT");
					result = string.Format(format, this.LeftNode.WhereString, logicString, this.RightNode.WhereString);
				}
				//
				return result;
			}
		}

		#endregion

		#region 构造方法

		public Comparer(Command command)
		{
			if(command == null)
			{
				throw new ArgumentNullException("Command");
			}
			this.Command = command;
		}

		/// <summary>
		/// 采用两个实体比较对象按照某种比较逻辑进行处理，构造一个新的实体比较对象
		/// </summary>
		/// <param name="comparer1">比较逻辑符号左边的实体比较对象</param>
		/// <param name="logic">比较逻辑</param>
		/// <param name="comparer2">比较逻辑符号左边的实体比较对象</param>
		public Comparer(Comparer comparer1, CompareLogic logic, Comparer comparer2)
		{
			if(object.Equals(comparer1, null))
			{
				throw new ArgumentNullException("参数 comparer1 不能都为空！");
			}
			if(object.Equals(comparer2, null) && logic != CompareLogic.Not)
			{
				throw new ArgumentNullException("参数 comparer2 为空的时候，只能是NOT操作！");
			}
			this.Command = comparer1.Command;
			this.LeftNode = comparer1;
			this.RightNode = comparer2;
			this.Logic = logic;
		}

		#endregion

		#region 静态方法

		public static Comparer Not(Comparer comparer)
		{
			return new Comparer(comparer, CompareLogic.Not, null);
		}

		private static bool IsEmpty(Comparer comparer)
		{
			if(object.Equals(comparer, null))
			{
				return true;
			}
			if(comparer.IsLeaf && string.IsNullOrEmpty(comparer.FieldName))
			{
				return true;
			}
			return false;
		}

		private static Comparer GenerateOperator(Comparer comparer, object value, CompareOperator @operator)
		{
			if(comparer.Command == null)
			{
				throw new ArgumentException("Comparer 关联的Command对象为空！");
			}
			var tnf = comparer.Command.TakeOneStackFields();
			comparer.FieldName = tnf.SqlFieldName;
			comparer.ParameterName = comparer.Command.CreateParameter(tnf, value);
			comparer.Operator = @operator;
			comparer.Command.fieldStack.Clear();
			return comparer;
		}

		#endregion

		#region 公共方法

		/// <summary>
		/// 将当前实体属性的值作为比较的值，得到一个新的实体比较对象
		/// </summary>
		/// <param name="field">实体对象的属性字段</param>
		/// <returns>比较表达式</returns>
		public Comparer EqualValue(object field)
		{
			var comparer = new Comparer(this.Command);
			var tnf = comparer.Command.TakeOneStackFields();
			comparer.FieldName = tnf.SqlFieldName;
			comparer.ParameterName = comparer.Command.CreateParameter(tnf, field);
			comparer.Operator = CompareOperator.Equal;
			comparer.Command.fieldStack.Clear();
			return comparer;
		}

		/// <summary>
		/// 判断指定字段条件为空 Is NULL
		/// </summary>
		/// <param name="field"></param>
		/// <returns></returns>
		public Comparer IsNull(object field)
		{
			var comparer = new Comparer(this.Command);
			comparer.FieldName = comparer.Command.TakeOneStackFields().Field;
			comparer.ParameterName = "NULL";
			comparer.Operator = CompareOperator.Is;
			comparer.Command.fieldStack.Clear();
			return comparer;
		}

		/// <summary>
		/// 判断指定字段条件为空 Is Not NULL
		/// </summary>
		/// <param name="field"></param>
		/// <returns></returns>
		public Comparer IsNotNull(object field)
		{
			var comparer = this.IsNull(field);
			comparer.Operator = CompareOperator.IsNot;
			return comparer;
		}

		/// <summary>
		/// 指定条件的包含范围
		/// </summary>
		/// <typeparam name="T">属性字段的类型</typeparam>
		/// <param name="field">属性字段</param>
		/// <param name="beginvalue">起始值</param>
		/// <param name="endvalue">结束值</param>
		/// <returns>比较对象</returns>
		public Comparer Between<T>(T field, T beginvalue, T endvalue)
		{
			var comparer = new Comparer(this.Command);
			var tnf = comparer.Command.TakeOneStackFields();
			comparer.FieldName = tnf.SqlFieldName;
			comparer.ParameterName = string.Format(" {0} AND {1} ", comparer.Command.CreateParameter(tnf, beginvalue), comparer.Command.CreateParameter(tnf, endvalue));
			comparer.Operator = CompareOperator.Between;
			comparer.Command.fieldStack.Clear();
			return comparer;
		}

		/// <summary>
		/// 根据实体对象的属性，获取新的条件比较对象，用于比较操作符重载
		/// </summary>
		/// <param name="field"></param>
		/// <returns></returns>
		public Comparer Property(object field)
		{
			var comparer = new Comparer(this.Command);
			return comparer;
		}

		/// <summary>
		/// 对一组CommandCompare 对象，执行CompareLogic 类型的比较，通常用于构造复杂的带括号的条件查询
		/// </summary>
		/// <param name="comparers">Command比较对象列表</param>
		/// <param name="logic">各组比较条件的组合方式，And，Or，Not</param>
		/// <returns>新的条件比较对象</returns>
		public Comparer Compare(List<Comparer> comparers, CompareLogic logic)
		{
			if(comparers == null || comparers.Count == 0)
			{
				throw new Exception("Command 条件比较对象集合不能为空或者空引用！");
			}
			if(comparers.Count == 1)
			{
				return comparers[0];
			}
			var comparer = new Comparer(this.Command);
			//将列表转换成树
			foreach(Comparer item in comparers)
			{
				if(object.Equals(comparer.LeftNode, null))
				{
					comparer.LeftNode = item;
					comparer.Logic = logic;
				}
				else if(object.Equals(comparer.RightNode, null))
				{
					comparer.RightNode = item;
				}
				else
				{
					var buffer = new Comparer(this.Command);
					buffer.LeftNode = comparer;
					buffer.Logic = logic;
					buffer.RightNode = item;
					comparer = buffer;
				}
			}
			return comparer;
		}

		/// <summary>
		/// 将当前实体属性的值和要比较的值进行比较，得到一个新的实体比较对象
		/// </summary>
		/// <param name="field">实体对象属性</param>
		/// <param name="@operator">比较类型枚举</param>
		/// <param name="value">要比较的值</param>
		/// <returns>比较表达式</returns>
		public Comparer Compare<T>(T field, CompareOperator @operator, T value)
		{
			return Compare<T>(field, @operator, value, null);
		}

		/// <summary>
		/// 将当前实体属性的值和要比较的值进行比较，得到一个新的实体比较对象
		/// </summary>
		/// <param name="field">实体对象属性</param>
		/// <param name="operator">比较类型枚举</param>
		/// <param name="value">要比较的值</param>
		/// <param name="format">SQL 函数格式串，例如 "DATEPART(hh, {0})"</param>
		/// <returns>比较表达式</returns>
		public Comparer Compare<T>(T field, CompareOperator @operator, T value, string format)
		{
			return ComparerInner<T>(field, @operator, value, format);
		}

		public Comparer Compare(object field, string @operator, Command value)
		{
			return Compare(field, GetOperator(@operator), value);
		}

		public Comparer Compare(object field, CompareOperator @operator, Command value)
		{
			var comparer = new Comparer(this.Command);
			var tnf = comparer.Command.TakeOneStackFields();
			comparer.FieldName = tnf.SqlFieldName;
			comparer.Operator = @operator;
			if(@operator == CompareOperator.Is || @operator == CompareOperator.IsNot)
			{
				throw new ArgumentOutOfRangeException("IS 操作符的不支持子查询！");
			}
			else
			{
				string childSql = value.ToString();
				if(value.Parameters.Count > 0)
				{
					//先备份SQL语句中的参数名
					foreach(string key in value.Parameters.Keys)
					{
						childSql = childSql.Replace(key, key + "_C");
					}
					foreach(string key in value.Parameters.Keys)
					{
						var vtnf = value.Parameters[key];
						string paraName = this.Command.CreateParameter(vtnf);
						childSql = childSql.Replace(key + "_C", paraName);
					}
				}
				comparer.ParameterName = "\r\n(" + childSql + ")\r\n";
			}
			comparer.Format = "";
			return comparer;
		}

		public Comparer Compare(object field, CompareOperator @operator, ChildFunc value)
		{
			Command childCommand = value(this.Command);
			return Compare(field, @operator, childCommand);
		}

		public Comparer Compare(object field, string @operator, ChildFunc value)
		{
			Command childCommand = value(this.Command);
			return Compare(field, GetOperator(@operator), childCommand);
		}

		public Comparer Compare<T>(T field, string @operator, T value)
		{
			return Compare<T>(field, @operator, value, null);
		}

		public Comparer Compare<T>(T field, string @operator, T value, string format)
		{
			return this.Compare<T>(field, GetOperator(@operator), value, format);
		}

		/// <summary>
		/// 将当前实体属性的值和要比较的数组值进行比较，得到一个新的实体比较对象
		/// </summary>
		/// <typeparam name="T">实体类属性类型</typeparam>
		/// <param name="field">实体类属性</param>
		/// <param name="operator">比较类型字符串</param>
		/// <param name="value">要比较的数组值</param>
		/// <param name="format">附加的SQL函数格式串</param>
		/// <returns></returns>
		public Comparer Compare<T>(T field, string @operator, T[] value, string format)
		{
			return this.Compare(field, GetOperator(@operator), value, format);
		}

		public Comparer Compare<T>(T field, CompareOperator @operator, T[] value)
		{
			return Compare<T>(field, @operator, value, null);
		}

		public Comparer Compare<T>(T field, string @operator, T[] value)
		{
			return Compare<T>(field, GetOperator(@operator), value, null);
		}

		public Comparer Compare<T>(T field, CompareOperator @operator, T[] value, string format)
		{
			if(value == null && (@operator == CompareOperator.In || @operator == CompareOperator.NotIn))
			{
				throw new ArgumentNullException("IN 条件的参数不能为空！");
			}
			var comparer = new Comparer(this.Command);
			var tnf = comparer.Command.TakeOneStackFields();
			comparer.FieldName = tnf.SqlFieldName;
			comparer.Operator = @operator;
			comparer.Format = format;
			if(@operator == CompareOperator.In || @operator == CompareOperator.NotIn)
			{
				string[] paraNames = new string[value.Length];
				Type type = field == null ? typeof(string) : field.GetType();
				if(type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid) || type.IsEnum)
				{
					for(int i = 0; i < value.Length; i++)
					{
						paraNames[i] = comparer.Command.CreateParameter(tnf, value[i]);
					}
					comparer.ParameterName = "(" + string.Join(",", paraNames) + ")";
				}
				else if(type.IsValueType)
				{
					for(int i = 0; i < value.Length; i++)
					{
						paraNames[i] = value[i].ToString();
					}
					comparer.ParameterName = "(" + string.Join(",", paraNames) + ")";
				}
				else
				{
					throw new ArgumentException("IN,Not In 操作只支持字符串数组或者值类型数组参数");
				}
			}
			else if(@operator == CompareOperator.Is || @operator == CompareOperator.IsNot)
			{
				comparer.ParameterName = "NULL";
			}
			else
			{
				throw new ArgumentException("当前方法只允许使用IN 或者 NOT IN，否则请使用另外的重载方法");
			}
			return comparer;
		}

		/// <summary>
		/// 将当前实体类的属性值应用SQL函数以后，与一个值进行比较。
		/// <example>
		/// <code>
		/// <![CDATA[
		///   //查询15点后登录的用户
		///   User user = new User();
		///
		///   Command command = Command.From(user)
		///				 .Select()
		///				 .Where(cmp => cmp.CompareSqlFunction(user.LastLoginTime, CommandCompare.CompareType.Greater, 15, "DATEPART(hh, {0})"))
		///				 .End();
		/// ]]>
		/// </code>
		/// </example>
		/// </summary>
		/// <typeparam name="T">属性的类型</typeparam>
		/// <param name="field">属性字段</param>
		/// <param name="operator">比较类型枚举</param>
		/// <param name="value">应用函数后要比较的值</param>
		/// <param name="format">SQL 函数格式串，例如 "DATEPART(hh, {0})"</param>
		/// <returns>比较表达式</returns>
		public Comparer CompareSqlFunction<T>(T field, CompareOperator @operator, object value, string format)
		{
			return ComparerInner<T>(field, @operator, value, format);
		}

		/// <summary>
		/// 将当前实体类的属性值应用SQL函数以后，与一个值进行比较。
		/// <example>
		/// <code>
		/// <![CDATA[
		///   //查询15点后登录的用户
		///   User user = new User();
		///
		///   Command command = Command.From(user)
		///				 .Select()
		///				 .Where(cmp => cmp.ComparerSqlFunction(user.LastLoginTime, ">", 15, "DATEPART(hh, {0})"))
		///				 .End();
		/// ]]>
		/// </code>
		/// </example>
		/// </summary>
		/// <typeparam name="T">属性的类型</typeparam>
		/// <param name="field">属性字段</param>
		/// <param name="operator">比较类型字符串</param>
		/// <param name="value">应用函数后要比较的值</param>
		/// <param name="format">SQL 函数格式串，例如 "DATEPART(hh, {0})"</param>
		/// <returns>比较表达式</returns>
		public Comparer CompareSqlFunction<T>(T field, string @operator, object value, string format)
		{
			return ComparerInner<T>(field, GetOperator(@operator), value, format);
		}

		public Comparer Count(CompareOperator @operator, object value)
		{
			Comparer comparer = new Comparer(this.Command);
			if(@operator == CompareOperator.Is || @operator == CompareOperator.In || @operator == CompareOperator.NotIn)
			{
				throw new ArgumentOutOfRangeException("IS,IN,NOT IN 操作符请使用Count 方法不受支持！");
			}
			comparer.FieldName = "*";
			comparer.Operator = @operator;
			comparer.ParameterName = comparer.Command.CreateParameter(null, value);
			comparer.Format = "COUNT({0})";
			return comparer;
		}

		public Comparer Count<T>(T field, CompareOperator @operator, object value)
		{
			return ComparerInner<T>(field, @operator, value, "COUNT({0})");
		}

		public Comparer Count<T>(T field, string @operator, object value)
		{
			return ComparerInner<T>(field, GetOperator(@operator), value, "COUNT({0})");
		}

		public Comparer Avg<T>(T field, CompareOperator @operator, object value)
		{
			return ComparerInner<T>(field, @operator, value, "AVG({0})");
		}

		public Comparer Avg<T>(T field, string @operator, object value)
		{
			return ComparerInner<T>(field, GetOperator(@operator), value, "AVG({0})");
		}

		public Comparer Max<T>(T field, CompareOperator @operator, T value)
		{
			return Compare<T>(field, @operator, value, "MAX({0})");
		}

		public Comparer Max<T>(T field, string @operator, T value)
		{
			return Compare<T>(field, GetOperator(@operator), value, "MAX({0})");
		}

		public Comparer Min<T>(T field, CompareOperator @operator, T value)
		{
			return Compare<T>(field, @operator, value, "MIN({0})");
		}

		public Comparer Min<T>(T field, string @operator, T value)
		{
			return Compare<T>(field, GetOperator(@operator), value, "MIN({0})");
		}

		public Comparer Sum<T>(T field, CompareOperator @operator, T value)
		{
			return Compare<T>(field, @operator, value, "SUM({0})");
		}

		public Comparer Sum<T>(T field, string @operator, T value)
		{
			return Compare<T>(field, GetOperator(@operator), value, "SUM({0})");
		}

		/// <summary>
		/// 清除字段堆栈,返回当前对象,如果在调用Comparer方法之前调用了关联的实体类属性进行条件判断,动态构造比较条件,此时请调用此方法
		/// </summary>
		/// <returns></returns>
		public Comparer Clear()
		{
			this.Command.fieldStack.Clear();
			return this;
		}

		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}

		#endregion

		#region 私有方法

		private string GetFieldString(string format, string fieldName)
		{
			var result = string.Empty;
			if(!string.IsNullOrEmpty(format))
			{
				if(format.Contains("--"))
				{
					throw new Exception("SQL 函数格式串中有危险的内容");
				}
				if(!format.Contains("{0}"))
				{
					throw new Exception("SQL 函数格式串未指定替换位置{0}");
				}
				result = string.Format(format, fieldName);
			}
			else
			{
				result = fieldName;
			}
			return result;
		}

		private string GetOperatorString()
		{
			return this.GetOperatorString(Operator);
		}

		private string GetOperatorString(CompareOperator @operator)
		{
			var result = string.Empty;
			switch(@operator)
			{
				case CompareOperator.Equal:
					result = "=";
					break;
				case CompareOperator.Greater:
					result = ">";
					break;
				case CompareOperator.Like:
					result = " LIKE ";
					break;
				case CompareOperator.LessThanOrEqual:
					result = "<=";
					break;
				case CompareOperator.GreaterThanOrEqual:
					result = ">=";
					break;
				case CompareOperator.NotEqual:
					result = "<>";
					break;
				case CompareOperator.LessThan:
					result = "<";
					break;
				case CompareOperator.In:
					result = " IN ";
					break;
				case CompareOperator.Is:
					result = " IS ";
					break;
				case CompareOperator.NotIn:
					result = " NOT IN ";
					break;
				case CompareOperator.IsNot:
					result = " IS NOT ";
					break;
				case CompareOperator.Between:
					result = " BETWEEN ";
					break;
				default:
					result = "=";
					break;
			}
			return result;
		}

		private CompareOperator GetOperator(string @operator)
		{
			@operator = String.IsNullOrEmpty(@operator) ? "=" : @operator.Trim().ToLower();
			//替换成枚举比较好
			CompareOperator result;
			switch(@operator)
			{
				case "=":
					result = CompareOperator.Equal;
					break;
				case ">":
					result = CompareOperator.Greater;
					break;
				case "<":
					result = CompareOperator.LessThan;
					break;
				case "<>":
					result = CompareOperator.NotEqual;
					break;
				case ">=":
					result = CompareOperator.GreaterThanOrEqual;
					break;
				case "<=":
					result = CompareOperator.LessThanOrEqual;
					break;
				case "like":
					result = CompareOperator.Like;
					break;
				case "is":
					result = CompareOperator.Is;
					break;
				case "is not":
					result = CompareOperator.IsNot;
					break;
				case "in":
					result = CompareOperator.In;
					break;
				case "not in":
					result = CompareOperator.NotIn;
					break;
				default:
					throw new Exception("比较符必须是 =, >, <, >=, <=, <>, LIKE, IS, IN, NOT IN 中的一种。");
			}
			return result;
		}

		private Comparer ComparerInner<T>(T field, CompareOperator @operator, object value, string format)
		{
			TableNameField leftField;
			TableNameField rightField;
			var comparer = new Comparer(this.Command);
			comparer.Command.TakeTwoStackFields(field, value, out leftField, out rightField);
			if(leftField != null)
			{
				comparer.FieldName = leftField.SqlFieldName;
			}
			else if(rightField != null)
			{
				comparer.FieldName = rightField.SqlFieldName;
			}
			else
			{
				throw new Exception("前或者后的比较字段对像均为空！");
			}
			comparer.Operator = @operator;
			if(@operator == CompareOperator.Is || @operator == CompareOperator.IsNot)
			{
				if(value != null)
				{
					var valueString = value.ToString().ToUpper().Trim();
					if(valueString == "NULL" || valueString == "NOT NULL")
					{
						comparer.ParameterName = valueString;
					}
					else
					{
						throw new ArgumentOutOfRangeException("IS 操作符的对象只能是NULL 或者 NOT NULL");
					}
				}
				else
				{
					comparer.ParameterName = "NULL";
				}
			}
			else if(@operator == CompareOperator.In || @operator == CompareOperator.NotIn)
			{
				throw new ArgumentOutOfRangeException("IN,NOT IN 操作符请使用Comparer方法中带数组参数的重载方法");
			}
			else
			{
				if(leftField != null && rightField != null)
				{
					//可能直接用相同的字段来比较，感谢网友Sharp_C 发现此问题
					if(leftField.SqlFieldName == rightField.SqlFieldName)
					{
						comparer.ParameterName = comparer.Command.CreateParameter(leftField, value);
					}
					else
					{
						comparer.ParameterName = rightField.SqlFieldName;
					}
				}
				else if(leftField != null && rightField == null)
				{
					comparer.ParameterName = comparer.Command.CreateParameter(leftField, value);
				}
				else if(leftField == null && rightField != null)
				{
					comparer.FieldName = comparer.Command.CreateParameter(rightField, field);
					comparer.ParameterName = rightField.SqlFieldName;
				}
				else
				{
					throw new InvalidOperationException("当前CommandCompare 内部操作状态无效，条件比较未使用实体类的属性。");
				}
			}
			comparer.Format = format;
			return comparer;
		}

		private bool CheckChildLogic(Comparer comparer, CompareLogic logic)
		{
			//currCmp 不能是叶子结点
			//如果子节点的逻辑类型不同于当前逻辑类型，直接返回 非
			if(comparer.Logic != logic)
			{
				return false;
			}
			//如果子节点的逻辑类型 同于当前逻辑类型，则需要检查子节点的左右子节点与当前逻辑类型的对比
			if(comparer.LeftNode.IsLeaf && comparer.RightNode.IsLeaf)
			{
				return comparer.Logic == logic;
			}
			else
			{
				if(!comparer.LeftNode.IsLeaf && !comparer.RightNode.IsLeaf)
				{
					bool leftFlag = false;
					bool rightFlag = false;
					leftFlag = CheckChildLogic(comparer.LeftNode, logic);
					rightFlag = CheckChildLogic(comparer.RightNode, logic);
					return leftFlag && rightFlag;
				}
				else if(!comparer.LeftNode.IsLeaf && comparer.RightNode.IsLeaf)
				{
					return CheckChildLogic(comparer.LeftNode, logic);
				}
				else if(comparer.LeftNode.IsLeaf && !comparer.RightNode.IsLeaf)
				{
					return CheckChildLogic(comparer.RightNode, logic);
				}
				else
				{
					return false;
				}
			}
		}

		#endregion

		#region 操作符重载

		/// <summary>
		/// 将两个实体比较对象进行逻辑 与 比较，得到一个新的实体比较表达式
		/// </summary>
		/// <param name="comparer1">左表达式</param>
		/// <param name="comparer2">右表达式</param>
		/// <returns>实体比较表达式</returns>
		public static Comparer operator &(Comparer comparer1, Comparer comparer2)
		{
			//处理条件累加问题某一侧对象可能为空的情况
			if(!IsEmpty(comparer1) && !IsEmpty(comparer2))
			{
				return new Comparer(comparer1, CompareLogic.And, comparer2);
			}
			else
			{
				if(IsEmpty(comparer1))
				{
					return comparer2;
				}
				else
				{
					return comparer1;
				}
			}
		}

		/// <summary>
		/// 将两个实体比较对象进行逻辑 与 比较，得到一个新的实体比较表达式
		/// </summary>
		/// <param name="comparer1">左表达式</param>
		/// <param name="comparer2">右表达式</param>
		/// <returns>实体比较表达式</returns>
		public static Comparer operator |(Comparer comparer1, Comparer comparer2)
		{
			//处理条件累加问题某一侧对象可能为空的情况
			if(!object.Equals(comparer1, null) && !object.Equals(comparer2, null))
			{
				return new Comparer(comparer1, CompareLogic.Or, comparer2);
			}
			else
			{
				if(object.Equals(comparer1, null))
				{
					return comparer2;
				}
				else
				{
					return comparer1;
				}
			}
		}

		/// <summary>
		/// 设置等于某个实体属性的比较条件
		/// </summary>
		/// <param name="comparer">当前实体比较对象</param>
		/// <param name="value">要比较的值</param>
		/// <returns>构造的实体比较对象</returns>
		public static Comparer operator ==(Comparer comparer, object value)
		{
			return GenerateOperator(comparer, value, CompareOperator.Equal);
		}

		/// <summary>
		/// 设置不等于某个实体属性的比较条件
		/// </summary>
		/// <param name="comparer">当前实体比较对象</param>
		/// <param name="value">要比较的值</param>
		/// <returns>构造的实体比较对象</returns>
		public static Comparer operator !=(Comparer comparer, object value)
		{
			return GenerateOperator(comparer, value, CompareOperator.NotEqual);
		}

		/// <summary>
		/// 设置不小于某个实体属性的比较条件
		/// </summary>
		/// <param name="comparer">当前实体比较对象</param>
		/// <param name="value">要比较的值</param>
		/// <returns>构造的实体比较对象</returns>
		public static Comparer operator >=(Comparer comparer, object value)
		{
			return GenerateOperator(comparer, value, CompareOperator.GreaterThanOrEqual);
		}

		/// <summary>
		/// 设置不大于某个实体属性的比较条件
		/// </summary>
		/// <param name="comparer">当前实体比较对象</param>
		/// <param name="value">要比较的值</param>
		/// <returns>构造的实体比较对象</returns>
		public static Comparer operator <=(Comparer comparer, object value)
		{
			return GenerateOperator(comparer, value, CompareOperator.LessThanOrEqual);
		}

		/// <summary>
		/// 设置大于某个实体属性的比较条件
		/// </summary>
		/// <param name="comparer">当前实体比较对象</param>
		/// <param name="value">要比较的值</param>
		/// <returns>构造的实体比较对象</returns>
		public static Comparer operator >(Comparer comparer, object value)
		{
			return GenerateOperator(comparer, value, CompareOperator.Greater);
		}

		/// <summary>
		/// 设置小于某个实体属性的比较条件
		/// </summary>
		/// <param name="comparer">当前实体比较对象</param>
		/// <param name="value">要比较的值</param>
		/// <returns>构造的实体比较对象</returns>
		public static Comparer operator <(Comparer comparer, object value)
		{
			return GenerateOperator(comparer, value, CompareOperator.LessThan);
		}

		#endregion
	}
}